<template>
  <a-modal
    v-model:visible="visible"
    draggable
    width="500px"
    @cancel="cancelMethod"
    @before-ok="handleBeforeOk"
  >
    <template #title> 编辑发票日志 </template>
    <a-form
      ref="formRef"
      :label-col-props="{ span: 6 }"
      :model="formData"
      :wrapper-col-props="{ span: 18 }"
      class="form"
    >
      <div v-if="isNew">
        <a-form-item field="auditNumber" label="OA单据号">
          <a-input
            v-model="formData.auditNumber"
            disabled
            :placeholder="$t('person.placeholder.name')"
          />
        </a-form-item>
        <a-form-item
          label="凭证报文"
          :rules="[{ required: true, message: '请输入凭证报文' }]"
          field="preVoucherContent"
        >
          <a-textarea v-model="formData.preVoucherContent" auto-size />
        </a-form-item>
      </div>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import {
    TUser,
    TUserDTO,
    useCreate,
    useDetail,
    useUpdateAuthMenus,
  } from '@/api/subsidy';
  import { useUserStore } from '@/store';
  import {
    preVoucherDetail,
    preVoucherEdit,
    PreVoucherQueryDTO,
    PreVoucherVO,
  } from '@/api/invoice';

  defineProps({
    title: {
      type: String,
      default: '人员管理',
    },
  });
  const formRef = ref();
  const visible = ref(false);
  const isNew = ref(true);
  const formData = ref<PreVoucherVO>({
    auditNumber: '',
    bizVoucherNumber: '',
    id: '',
    preVoucherContent: '',
    sucessFlag: '',
  });
  const getDetailMethod = async (id: string) => {
    const { data } = await preVoucherDetail(id);
    formData.value = data;
  };

  function handleChangeVisible(obj: PreVoucherVO) {
    visible.value = !visible.value;
    if (obj) {
      getDetailMethod(obj.id);
    }
  }

  const cancelMethod = () => {
    visible.value = false;
  };
  const emit = defineEmits(['refresh']);

  function handleBeforeOk(done: any) {
    formRef.value.validate((valid: any) => {
      if (valid) {
        done(false);
      } else {
        preVoucherEdit(formData.value)
          .then(() => {
            Message.success('操作成功');
            emit('refresh');
            done();
          })
          .catch(() => {
            done(false);
          });
      }
    });
  }

  defineExpose({ handleChangeVisible });
</script>
